package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.services.VisitorServiceImpl;

/**
 * Servlet implementation class DeleteVisitorServlet.
 */
@WebServlet("/DeleteVisitorServlet")
public class DeleteVisitorServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * Instantiates a new delete visitor servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public DeleteVisitorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
				
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		int idVisitor = Integer.parseInt(request.getParameter("visitorID"));
		boolean check = false;
		
		try{		
			VisitorServiceImpl vis = new VisitorServiceImpl();
			
			check = vis.deleteVisitor(idVisitor);
       
		 }catch(Exception theException){
		        System.out.println(theException); 
		 }
		
		  if(check){
			  session.setAttribute("Session", 1);
			  ListAllVisitorsServlets listVisitors = new ListAllVisitorsServlets();
			  listVisitors.doGet(request, response);  
			  
		  }
		   else{
			   session.setAttribute("Session", 2);
			   RequestDispatcher rd=request.getRequestDispatcher("ListVisitors.jsp");  
			   rd.forward(request,response);  
		   }
	}

}
